//
//  STChannelCell.swift
//  StarTimesON
//
//  Created by Toj on 9/26/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

protocol STChannelCellDelegate: NSObjectProtocol {
    func channelCellDidTapSelected(_ view: STChannelCell)
    func channelCellDidTapDiscount(_ view: STChannelCell)
}

extension STChannelCellDelegate {
    func channelCellDidTapDiscount(_ view: STChannelCell) { }
}

class STChannelCell: UICollectionViewCell {
    
    weak var delegate: STChannelCellDelegate?
    
    var model: STChannelCellModel? { didSet {
        guard let channel = model else { return }
        // print("-->toj: MBChannelCell isCarCell: \(channel.isCarCell)")
        nameLabel.text = channel.name
        discountView.isHidden = !channel.isPayPromotion
        discountLabel.text = channel.promotionTag
        
        selectedButton.isSelected = channel.isSelected
        selectedButton.isHidden = channel.isCarTitleCell
        lineView.isHidden = channel.isOnlyOne
        
        let url = URL(string: channel.iconUrl)
        iconImageView.kf.setImage(with: url, placeholder: "pay_ment_default".to_image)
        
        rechargeLabel.isHidden = true
        if channel.isEWallet {
            rechargeLabel.isHidden = !channel.isNeedRecharge
        }
        setupRelayoutDiscount()
    } }
    
    @objc private func didTapRechargeWallet() {
        STSRouter.openLocalPage(.walletRecharge)
    }
    
    @objc private func didTapSelected() {
        guard model?.isCarTitleCell == false else { return }
        delegate?.channelCellDidTapSelected(self)
    }
    
    @objc private func didTapDiscount() {
        delegate?.channelCellDidTapDiscount(self)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let tap = UITapGestureRecognizer(target: self, action: #selector(didTapSelected))
        contentView.addGestureRecognizer(tap)
        setupAddSubViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupLayoutSubViews()
    }
    
    private lazy var leftShadowView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_channel_shadow_left".to_image
        return view
    }()
    
    private lazy var rightShadowView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_channel_shadow_right".to_image
        return view
    }()
    
    private lazy var iconImageView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFit
        return view
    }()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont.bold(with: 12.ui_scale)
        view.textColor = "#666666".to_color
        return view
    }()
    
    private lazy var discountLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont.bold(with: 10.ui_scale)
        view.textColor = "#99773C".to_color
        return view
    }()
    
    private lazy var discountView: UIButton = {
        let view = STButton()
        view.disableHighlighted = true
        view.setBackgroundImage("commodity_channel_discount".to_image, for: .normal)
        view.addTarget(self, action: #selector(didTapDiscount), for: .touchUpInside)
        return view
    }()
    
    private lazy var rechargeLabel: UIButton = {
        let view = STButton()
        view.disableHighlighted = true
        view.addTarget(self, action: #selector(didTapRechargeWallet), for: .touchUpInside)
        
        let attributedString = "recharge_".localized().to_attributedString(font: UIFont.systemFont(ofSize: 13.f_scale), color: STColor.olBlue, lineStyle: .underline)
        view.setAttributedTitle(attributedString, for: .normal)
       
        return view
    }()
    
    private lazy var selectedButton: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(resourceName: "com_radio_gold_nor"), for: .normal)
        view.setImage(UIImage(resourceName: "com_radio_gold_sel"), for: .selected)
        view.isUserInteractionEnabled = false
        return view
    }()
    
    private lazy var lineView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_channel_cell_line".to_image
        return view
    }()
}

extension STChannelCell {
    
    private func setupAddSubViews() {
        contentView.addSubview(leftShadowView)
        contentView.addSubview(rightShadowView)
        
        contentView.addSubview(iconImageView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(discountView)
        contentView.addSubview(rechargeLabel)
        contentView.addSubview(selectedButton)
        contentView.addSubview(lineView)
        discountView.addSubview(discountLabel)
    }
    
    private func setupLayoutSubViews() {
        
        leftShadowView.snp.makeConstraints { make in
            make.left.equalTo(contentView).inset(7)
            make.width.equalTo(49)
            make.top.bottom.equalTo(contentView)
        }
        
        rightShadowView.snp.makeConstraints { make in
            make.right.equalTo(contentView).inset(7)
            make.width.top.bottom.equalTo(leftShadowView)
        }
        
        iconImageView.snp.makeConstraints { make in
            make.left.equalTo(28.ui_scale)
            make.centerY.equalTo(contentView)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(6.ui_scale)
            make.centerY.equalTo(contentView)
        }
        
        selectedButton.snp.makeConstraints { make in
            make.right.equalTo(contentView).inset(28.ui_scale)
            make.centerY.equalTo(contentView)
            make.width.equalTo(20)
            make.height.equalTo(20)
        }
        
        rechargeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(contentView)
            make.right.equalTo(selectedButton.snp.left).offset(-10.ui_scale)
        }
        setupRelayoutDiscount()
    }
    
    private func setupRelayoutDiscount() {
        var left = 28.ui_scale
        var width = STScreenWidth - left * 2
        if selectedButton.isHidden {
            left = 56.ui_scale
            width = STScreenWidth - left - 28.ui_scale
        }
        lineView.snp.remakeConstraints { make in
            make.left.equalTo(left)
            make.width.equalTo(width)
            make.height.equalTo(1)
            make.bottom.equalTo(contentView)
        }
        
//        width = discountLabel.text?.st_stringWidth(font: discountLabel.font, maxHeight: 10) ?? 0
//        discountLabel.snp.remakeConstraints { make in
//            make.left.equalTo(nameLabel.snp.right).offset(8.ui_scale)
//            make.top.equalTo(iconImageView)
//            make.width.equalTo(width).priority(.high)
//            make.height.equalTo(10)
//            make.right.lessThanOrEqualTo(selectedButton.snp.left).offset(-8.ui_scale)
//        }
//        discountView.snp.remakeConstraints { make in
//            make.left.equalTo(discountLabel).inset(-4.ui_scale)
//            make.right.equalTo(discountLabel).inset(-3.ui_scale)
//            make.height.equalTo(17)
//            make.top.equalTo(discountLabel.snp.top).offset(-2)
//        }
    }
}
